//
//  ViewController.m
//  itaka
//
//  Created by Robert Soszynski on 1/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController 
@synthesize delegate;

- (IBAction)textFieldReturn:(id)sender
{
	[sender resignFirstResponder];
}

- (IBAction)backgroundTouched:(id)sender
{
	[mUsernameField resignFirstResponder];
    [mPasswordField resignFirstResponder];
}

- (IBAction) signIn {
    [self changeView: LOADING_VIEW];
    [[self delegate] signInButtonPressed: mUsernameField.text: mPasswordField.text: mDomain.text];
}

- (IBAction) signOut {
    [self changeView: START_VIEW];
    [[self delegate] signOutButtonPressed];
}

- (void) changeView: (enum views) view {
    [mSignIn setEnabled:FALSE];
    [mSignOut setEnabled:FALSE];
    [mUsernameField setHidden:TRUE];
    [mDomain setHidden:TRUE];
    [mPasswordField setHidden:TRUE];
    [mSignInInfo setHidden:TRUE];
    [mSignInLogin setHidden:TRUE];
    [mActivityIndicator stopAnimating];
    
    switch (view) {
        case START_VIEW:
            [mSignIn setEnabled:TRUE];
            [mUsernameField setHidden:FALSE];
            [mDomain setHidden:FALSE];
            [mPasswordField setHidden:FALSE];
            break;
        case LOADING_VIEW:
            [mActivityIndicator startAnimating];
            [mUsernameField resignFirstResponder];
            [mPasswordField resignFirstResponder];
            break;
        case SIGNED_VIEW:
            [mSignOut setEnabled:TRUE];
            [mSignInInfo setHidden:FALSE];
            [mSignInLogin setHidden:FALSE];
            mUsernameField.text = @"";
            mPasswordField.text = @"";
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void) signingCompleted: (NSString*) login {
    mSignInLogin.text = login;
    [self changeView: SIGNED_VIEW];
}

- (void) signingFailed {
    mSignInLogin.text = @"";
    [self changeView: START_VIEW];
}

@end
